/*
 * Copyrignt (c) xuzeshui.com. All Rights Reserved.
 * Author: Zeshui Xu<i@xuzeshui.com>
 * Created Time: 2015-12-04 10:25:12
 * Last Modified: 2016-09-21 11:56:22
 * File Name: db/mysql.go
 * Description: 抽离出Mysql单独的操作
 */
package db

import (
	"fmt"

	"doubimeizhi.com/utility"

	"github.com/astaxie/beego"
)

var (
	mysqlHelper *utility.MySQLHelper
)

func createMySQLHelper() bool {
	runmode := beego.AppConfig.DefaultString("runmode", "prod")
	mysqlHost := beego.AppConfig.DefaultString(fmt.Sprintf("mysql_%s::host", runmode), "127.0.0.1")
	mysqlPort := beego.AppConfig.DefaultInt(fmt.Sprintf("mysql_%s::port", runmode), 4100)
	mysqlUser := beego.AppConfig.DefaultString(fmt.Sprintf("mysql_%s::user", runmode), "root")
	mysqlPass := beego.AppConfig.DefaultString(fmt.Sprintf("mysql_%s::password", runmode), "")
	mysqlDB := beego.AppConfig.DefaultString(fmt.Sprintf("mysql_%s::db", runmode), "scloud_info")
	maxOpenConns := beego.AppConfig.DefaultInt(fmt.Sprintf("mysql_%s::maxOpenConns", runmode), 10)
	maxIdleConns := beego.AppConfig.DefaultInt(fmt.Sprintf("mysql_%s::maxIdleConns", runmode), 5)
	mysqlHelper = utility.NewMySQLHelper(mysqlHost, mysqlPort, mysqlUser, mysqlPass, mysqlDB, maxOpenConns, maxIdleConns)
	return true
}

func createScloudInfoMySQLHelper() bool {
	runmode := beego.AppConfig.DefaultString("RunMode", "prod")
	mysqlHost := beego.AppConfig.DefaultString(fmt.Sprintf("mysql_%s::mysql_host", runmode), "127.0.0.1")
	mysqlPort := beego.AppConfig.DefaultInt(fmt.Sprintf("mysql_%s::mysql_port", runmode), 4100)
	mysqlUser := beego.AppConfig.DefaultString(fmt.Sprintf("mysql_%s::mysql_user", runmode), "root")
	mysqlPass := beego.AppConfig.DefaultString(fmt.Sprintf("mysql_%s::mysql_password", runmode), "")
	mysqlDB := beego.AppConfig.DefaultString(fmt.Sprintf("mysql_%s::mysql_db", runmode), "wisdom_mall")
	maxOpenConns := beego.AppConfig.DefaultInt(fmt.Sprintf("mysql_%s::maxOpenConns", runmode), 10)
	maxIdleConns := beego.AppConfig.DefaultInt(fmt.Sprintf("mysql_%s::maxIdleConns", runmode), 5)
	mysqlHelper = utility.NewMySQLHelper(mysqlHost, mysqlPort, mysqlUser, mysqlPass, mysqlDB, maxOpenConns, maxIdleConns)
	return true
}

func GetScloudInfoMySQLHelper() *utility.MySQLHelper {
	if mysqlHelper == nil {
		createScloudInfoMySQLHelper()
	}
	return mysqlHelper
}



/*
	获取Mysql操作的句柄
*/
func GetMysqlHelper() *utility.MySQLHelper {
	return mysqlHelper
}

func init() {
	createMySQLHelper()
}
